package com.bawei.fuxi.base.http;

import com.bawei.fuxi.base.interceptor.MyInterceptor;
import com.bawei.fuxi.base.interceptor.TokenInterceptor;
import com.bawei.fuxi.base.utils.ConstantUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitManager {
    private RetrofitManager(){}
    private static RetrofitManager retrofitManager;
    public static RetrofitManager getInstance(){
        if(retrofitManager == null){
            synchronized (RetrofitManager.class){
                if(retrofitManager == null){
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }

    private Retrofit mRetrofit;
    public Retrofit getRetrofit() {
        if(mRetrofit == null){
            creataRetrofit();
        }
        return mRetrofit;
    }
    //创建Retrofit
    private void creataRetrofit() {

        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new MyInterceptor())//自定义拦截器
                .addInterceptor(new TokenInterceptor())//token
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))//log
                .build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(ConstantUtils.BASE_URL)
                .client(client)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//rxjava工厂
                .addConverterFactory(GsonConverterFactory.create())//Gson解析
                .build();
    }


}
